#include<vector>
#include<algorithm>
#include<cstdio>
#include<iostream>
using namespace std;

struct aplct{
    int id;
    int ge;
    int gi;
    float fnl;
    int rak;
    vector<int> choice;
};
struct admit{
    int num;
    vector<aplct> man;
};
bool cmp(aplct a,aplct b){
    if(a.fnl!=b.fnl)return (a.fnl>b.fnl);
    return a.ge>b.ge;
}
bool cmp2(aplct a,aplct b){
    return a.id<b.id;
}
vector<admit> mark;
vector<aplct> stu;
int main(){
    freopen("in.txt","r",stdin);
    int N,M,K;
    scanf("%d %d %d",&N,&M,&K);
    mark.resize(M);
    stu.resize(N);
    for(int i=0;i<M;i++){scanf("%d",&mark[i].num);}//M
    for(int i=0;i<N;i++){
        scanf("%d %d",&stu[i].ge,&stu[i].gi);
        stu[i].fnl=(stu[i].ge+stu[i].gi)/2;
        stu[i].id=i;
        int choices;
        for(int j=0;j<K;j++){
            scanf("%d",&choices);
            stu[i].choice.push_back(choices);
        }
    }//N*K
    sort(stu.begin(),stu.end(),cmp);//NlogN
    int seq=0;
    for(int i=0;i<N;i++){
        if(i>0 && stu[i].fnl==stu[i-1].fnl && stu[i].ge==stu[i-1].ge){
            seq--;
        }
        else{seq=0;}
        stu[i].rak=seq+i+1;
    }//N
    for(int i=0;i<N;i++){
        for(int j=0;j<stu[i].choice.size();j++){
            int aim=stu[i].choice[j];
            if(mark[aim].num==0 && mark[aim].man.back().rak==stu[i].rak){
                mark[aim].man.push_back(stu[i]);
                break;
            }
            else if(mark[aim].num>0){
                mark[aim].man.push_back(stu[i]);
                mark[aim].num--;
                break;
            }
        }
    }//N
    for(int i=0;i<mark.size();i++){
        if(mark[i].man.empty()){printf("\n");continue;}
        sort(mark[i].man.begin(),mark[i].man.end(),cmp2);
        for(int j=0;j<mark[i].man.size();j++){
            if(j==0){printf("%d",mark[i].man[j].id);}
            else{printf(" %d",mark[i].man[j].id);}
        }
        printf("\n");
    }//M
    return 0;
}
